#include <iostream>

class Queue {
public:
  Queue(int size = 20) {
    _pdata = new int[size];
    _front = _rear = 0;
    _size = size;
  }
  ~Queue() {
    delete[] _pdata;
    _pdata = nullptr;
  }

  Queue(const Queue &other) {
    _size = other._size;
    _front = other._front;
    _rear = other._rear;
    _pdata = new int[_size];
    for (int i = _front; i != _rear; i = (i + 1) % _size) {
      _pdata[i] = other._pdata[i];
    }
  }

  Queue &operator=(const Queue &other) {
    if (this == &other)
      return *this;

    delete[] _pdata;

    _size = other._size;
    _front = other._front;
    _rear = other._rear;
    _pdata = new int[_size];
    for (int i = _front; i != _rear; i = (i + 1) % _size) {
      _pdata[i] = other._pdata[i];
    }
    return *this;
  }

  // 在提供了拷贝构造和拷贝赋值操作后，可以不提供移动构造和移动赋值，同时编译器也不会提供对应的默认实现

  void push(int val) {
    if (full()) {
      resize();
    }
    _pdata[_rear] = val;
    _rear = (_rear + 1) % _size;
  }

  void pop() {
    if (empty())
      return;
    _front = (_front + 1) % _size;
  }

  int top() { return _pdata[_front]; }
  bool full() { return (_rear + 1) % _size == _front; }
  bool empty() { return _front == _rear; }

private:
  int *_pdata;
  int _front;
  int _rear;
  int _size;

private:
  void resize() {
    int *ptmp = new int[2 * _size];
    int idx = 0;
    for (int i = _front; i != _rear; i = (i + 1) % _size) {
      ptmp[idx++] = _pdata[i];
    }
    delete[] _pdata;
    _pdata = ptmp;
    _front = 0;
    _rear = idx;
    _size = 2 * _size;
  }
};

int main() {
  Queue queue;
  for (int i = 0; i < 20; i++) {
    queue.push(i);
  }

  while (!queue.empty()) {
    std::cout << queue.top() << " ";
    queue.pop();
  }
  std::cout << std::endl;

  return 0;
}